package org.ak47.spring.boot.grpc.test.rpc;


import io.grpc.stub.StreamObserver;
import org.ak47.spring.boot.grpc.annotations.GrpcService;
import org.ak47.spring.boot.grpc.test.api.None;
import org.ak47.spring.boot.grpc.test.api.RPCDateRequest;
import org.ak47.spring.boot.grpc.test.api.RPCDateResponse;
import org.ak47.spring.boot.grpc.test.api.RPCDateServiceGrpc;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * <p> </p>
 *
 * @author hexiaohong
 * @since 2019/4/10
 */
@GrpcService
public class RPCDateService extends RPCDateServiceGrpc.RPCDateServiceImplBase
{
    @Override
    public void getDate(RPCDateRequest request, StreamObserver<RPCDateResponse> responseObserver)
    {
        RPCDateResponse rpcDateResponse = null;
        Date now=new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");
                String nowTime = simpleDateFormat.format( now );
        try {
            rpcDateResponse = RPCDateResponse
                    .newBuilder()
                    .setServerDate( "Welcome " + request.getUserName()  + ", " + nowTime )
                    .build();
        } catch (Exception e) {
            responseObserver.onError(e);
        } finally {
            responseObserver.onNext( rpcDateResponse );
        }
        responseObserver.onCompleted();
    }

    @Override
    public void getByID(None request, StreamObserver<RPCDateResponse> responseObserver)
    {
        super.getByID(request, responseObserver);
    }
}
